import 'dart:async';

import 'package:flutter/material.dart';

import 'ScalePainter.dart';
import 'ScaleScrollController.dart';
/**
 * Create by laoge
 * on 2021/4/25 0025
 */

typedef ScaleValueChangeListener = void Function(String value);

class ScaleWidget extends StatefulWidget {
  static const Duration defaultDuration = Duration(milliseconds: 200);

  final double width;

  final double height;

  final int lineInterval;

  final double interval;

  final double max;

  final double min;

  final ScaleValueChangeListener onChange;

  final ScrollController controller;

  final Color bgColor;

  final bool showTopLine;

  ScaleWidget(this.width, this.height,
      {this.controller,
      this.lineInterval = 10,
      this.interval = 1,
      this.max = 100,
      this.min = 0,
      this.onChange,
      this.bgColor = Colors.transparent,
      this.showTopLine = true});

  _ScaleWidget scaleWidget;

  @override
  _ScaleWidget createState() {
    return scaleWidget = _ScaleWidget();
  }
}

class _ScaleWidget extends State<ScaleWidget> {
  ScrollController scrollController;

  String value = '0';

  @override
  void initState() {
    // TODO: implement initState
    if (widget.controller == null) {
      scrollController =
          ScaleScrollController(widget.lineInterval, widget.interval);
    } else {
      scrollController = widget.controller;
    }
    scrollController.addListener(() {
      // print(scrollController.offset);
      value = (scrollController.offset / widget.lineInterval * widget.interval)
          .toStringAsFixed(1);
      if (null != widget.onChange) {
        widget.onChange(value);
      }
    });
    super.initState();
  }

  Timer timer;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Center(
          child: Listener(
            onPointerDown: (detail){
              if(null != timer){
                timer.cancel();
              }
            },
            child: NotificationListener(
              onNotification: (notification) {
                if ((notification is ScrollEndNotification)) {
                  // print("停止");
                  double offset = scrollController.offset;
                  var i = offset % widget.lineInterval;
                  int j = offset ~/ widget.lineInterval * widget.lineInterval;
                  int target = 0;
                  if (i >= widget.lineInterval / 2) {
                    target = j + widget.lineInterval;
                  } else {
                    target = j;
                  }

                  timer = Timer.periodic(Duration(milliseconds: 16), (timer) {
                    scrollController.animateTo(target.toDouble(),
                        duration: Duration(milliseconds: 100),
                        curve: Curves.easeOut);
                    timer.cancel();
                  });
                }
                return false;
              },
              child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                controller: scrollController,
                child: CustomPaint(
                  painter: ScalePainter(
                      context,
                      widget.width,
                      widget.lineInterval,
                      widget.min,
                      widget.max,
                      widget.interval,
                      fontSize: 13,
                      bgColor: widget.bgColor,
                      showTopLine: widget.showTopLine),
                  size: Size(
                      widget.lineInterval *
                          ((widget.max - widget.min) / widget.interval) +
                          widget.width,
                      widget.height),
                ),
              ),
            ),
          ),
        ),
        Center(
          child: Container(
            width: 5,
            height: widget.height,
            child: Column(
              children: [
                Expanded(
                    flex: 1, child: Container(width: 1, color: Colors.blue)),
                SizedBox(
                  height: 5,
                ),
                Expanded(
                    flex: 2, child: Container(width: 3, color: Colors.blue)),
                SizedBox(
                  height: 5,
                ),
                Expanded(
                    flex: 1, child: Container(width: 1, color: Colors.blue)),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
